<?php
// load the config
include_once 'admin/config.php';
$blog_db = "admin/" . $blog_db;

/* -----------------GET BASIC DATA--------------------*/
// load in a data file
$data = parse_ini_file($blog_db, true);
// does this work?
if (isset($data) == 0) { echo "Your data.ini file is not accessible!"; exit; }

// count them up
$number_of_entries = count($data);
krsort($data);

// allows for sequential access as well as random access
$n=0;
foreach($data as $unix_time => $array) {
		$numbered[$n] = $array;
		$numbered[$n]['id'] = $unix_time;
		$n++;		
}

/* ------------------------------ BEGIN FUNCTION DECLARATIONS ----------------------------------*/
function parse($content) {
	// startup
	$regexURL = "((http|https|ftp|mailto):\/\/[A-Za-z0-9\.\:\@\?\&\~\%\=\+\-\/\_\;\#]+)";
	$content = stripslashes(str_replace("<","&lt;",str_replace(">","&gt;","\n".$content)));

	// php-specific
	$content = str_replace("\r\n","\n",$content);

	// pictures [url]
	$content = preg_replace("/\[($regexURL\.(png|gif|jpg|PNG|GIF|JPG|jpeg|JPG)) ?([^]]+)?\]/i","<img src=\"\\1\" class=\"wikiimage\" alt=\"\\5\"/>",$content);
    
	// [url link] external links
	$content = preg_replace("/\[$regexURL ([^]]+)\]/i","<a href=\"\\1\">\\3</a>", $content);
	
	// plain urls in the text
	$content = preg_replace("/(?<![\"\[])$regexURL(?!\")/","<a href=\"\\0\">\\0</a>",$content);

	// lists <ul>
	$content = preg_replace("/(?<=\n)\* (.+)\n/","<uli>\\1</uli>",$content);
	$content = preg_replace("/<uli>(.+)\<\/uli>/","</p><ul>\\0</ul><p>\n",$content);
	// lists <ol>
	$content = preg_replace("/(?<=\n)# (.+)\n/","<oli>\\1</oli>",$content);
	$content = preg_replace("/<oli>(.+)\<\/oli>/","</p><ol>\\0</ol><p>\n",$content);
    // ... cleaning lists
	$content = str_replace("<oli>","<li>",$content);
	$content = str_replace("</oli>","</li>",$content);
	$content = str_replace("<uli>","<li>",$content);
	$content = str_replace("</uli>","</li>",$content);
    
	// text decorations 
	$content = preg_replace("/\*([^\n]+)\*/U","<strong>\\1</strong>", $content);
	$content = preg_replace("/\_([^\n]+)\_zx/U","<em>\\1</em>", $content);
	$content = preg_replace("/_\((.+)\)/U","<sub>\\1</sub>", $content);
	$content = preg_replace("/\^\((.+)\)/U","<sup>\\1</sup>", $content);
	$content = preg_replace("/\[\[(.+)\]\]/","<span class=\"sidenote\">\\1</span>",$content); 

	// Headlines <h1><h2><h3>
	$content = preg_replace("/\n(&equals;&equals;&equals;)(.+)\n/","</p><h4>\\2</h4><p>\n",$content);
	$content = preg_replace("/\n(&equals;&equals;)(.+)\n/","</p><h3>\\2</h3><p>\n",$content);
	$content = preg_replace("/\n(&equals;)(.+)\n/","</p><h2>\\2</h2><p>\n",$content);

	
	// strip leading and ending line breaks
	$content = preg_replace("/^(\n+)/","",$content); 
	$content = preg_replace("/\n{3,}/","\n",$content); 
	return $content;
}

/* SHOW LOTS */
function show_lots($start, $end) {
	global $numbered;
	global $blog_heading_st_tag;	
	global $blog_heading_end_tag;	
	if (($end)  > count($numbered)) { $end = count($numbered); }
	for ($i=$start; $i<$end; $i++) {

		echo "\n" . $blog_heading_st_tag;
		echo $numbered[$i]['title'];
		echo $blog_heading_end_tag . "\n";
		
		echo '<p>' . "\n";
		$content = $numbered[$i]['content'];
		$content = parse($content);
		
		echo $content . "\n";
		echo '</p>' . "\n";

		echo '<span style="font-size:90%;">';
		echo date('l\, F dS Y h:i A', $numbered[$i]['id']);
		echo '</span>';
		echo "\n\n";
	}
}

/* SHOW ONE */
function show_one($id) {
	global $data;
	global $blog_heading_st_tag;	
	global $blog_heading_end_tag;		
	if (array_key_exists($id, $data)) {
		
		$content = parse($data[$id]['content']);	
		$title = parse($data[$id]['title']);
		
		echo "\n" . $blog_heading_st_tag;
		echo $title;
		echo $blog_heading_end_tag . "\n";
	
		echo '<p>' . "\n";
		echo $content . "\n";
		echo '</p>' . "\n";

		echo '<span style="font-size:90%;">';
		echo date('l\, F dS Y h:i A', $id);
		echo '</span>';
		echo "\n\n";
		

	}
	else echo "<p><br /></p><div style=\"text-align:center;\"><em>Post ID</em> <strong>$id</strong><em> not found!</em></div>";
}


/* SHOW PAGE */
function show_page($page) {
	global $blog_posts_per_page;
	show_lots(($page-1) *$blog_posts_per_page, $page*$blog_posts_per_page);
}



/* =================AGREGATION FUNCTIONS=================*/
// SHOW ATOM
function show_atom() {
	global $numbered;
	global $blog_url;
	for ($i=0; $i<10; $i++) {
		echo "\n" . '<entry>' . "\n";
		
		echo "\t" . '<title type="html">';
		echo $numbered[$i]['title'];
		echo '</title>' . "\n";
		
		echo "\t" . '<link href="';
		echo $blog_url . 'index.php?post=' . $numbered[$i]['id'] . '" />' . "\n";
		
		echo "\t" . '<updated>';
		echo date(DATE_ATOM, $numbered[$i]['id']);
		echo '</updated>' . "\n";
		
		echo "\t" . '<id>';
		echo $blog_url . 'index.php?post=' . $numbered[$i]['id'] . '</id>' . "\n";
		
		echo "\t" . '<summary type="html">';
		
		echo htmlspecialchars($numbered[$i]['content']);
		echo '</summary>' . "\n";
		
		echo '</entry>';
	}
}



/* SHOW PAGE NAV */
function show_nav($current_page) {
    global $blog_controller;
	global $blog_posts_per_page;
	global $numbered;
	echo '<div class="nav">';
        echo "page: ";
        $total_pages = ceil((count($numbered))/$blog_posts_per_page);
        if ($current_page != 1) {
                        echo "<a href=\"index.php?page=";
                        echo ($current_page-1);
                        echo "\">&nbsp;&laquo; Prev&nbsp;</a>\n";
        }
        for ($i=1; $i<($total_pages+1); $i++) {
                if ($current_page == $i) {
                        echo "<span class=\"current\">$i</span>";
                }
                else { echo "<a href=\"index.php?page=$i\">$i</a>\n";}
        }
        if ($current_page < $total_pages){
        echo "<a href=\"index.php?page=";
                        echo ($current_page+1);
        echo "\">&nbsp;Next &raquo;&nbsp;</a>\n";
        }
      echo '</div>';
}

/* VERSION PRINTER */
function print_version(){
	echo "joleblog 0.7.5 non-release";
}

?>
